/**
 * 
 */
package de.cbf.cam.dsa.tavern.carte.model.persistence.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;

/**
 * @author Stefan Alter
 *
 */
public class CompositeFileConfig extends Composite {

	private Text 	textFileName 			= null;
	private Button	buttonChangeFileName	= null;
//	private Button 	buttonOpen				= null;
	private Button 	buttonSave				= null;

	/**
	 * @param parent
	 * @param style
	 */
	public CompositeFileConfig(Composite parent, int style) {
		super(parent, style);
		initialize();		
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {   
		GridLayout gridLayout = new GridLayout();
	        gridLayout.numColumns 			= 4;
	        gridLayout.horizontalSpacing	= 0;
	        gridLayout.verticalSpacing 		= 0;
	        gridLayout.marginWidth			= 0;	        
	        gridLayout.marginHeight			= 1;
        GridData gDTextFileName = new GridData();
	        gDTextFileName.horizontalAlignment = GridData.FILL;
	        gDTextFileName.verticalAlignment = GridData.CENTER;
	        gDTextFileName.grabExcessHorizontalSpace = true;
	        gDTextFileName.widthHint = 100;
	    //    
        this.setLayout(gridLayout);
        //
        textFileName = new Text(this, SWT.BORDER|SWT.READ_ONLY);
        textFileName.setLayoutData(gDTextFileName);
        //
        buttonChangeFileName = new Button(this, SWT.NONE);
        buttonChangeFileName.setText("...");
        buttonChangeFileName.addSelectionListener(new ChangeFileNameAdvancer());
        //
//        buttonOpen = new Button(this, SWT.NONE);
//        buttonOpen.setText("Op");
        //
        buttonSave = new Button(this, SWT.NONE);
        buttonSave.setText("Sv");              
        //
        registerListeners();
        //
        init();       		
	}

	private void init() {
		 buttonSave.setEnabled(false);		
		 textFileName.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
	}

	private void registerListeners() {
//		 buttonOpen.addSelectionListener(new OpenFileAdvancer());
		 buttonSave.addSelectionListener(new SaveFileAdvancer());
	}

	public String getFileName(){
		return textFileName.getText();
	}
	public void setFileName(String fileName){
		textFileName.setText(fileName);
		textFileName.setSelection(fileName.length());
	}
	
	public void setButtonChangeEnbaled(boolean enabled){
		buttonChangeFileName.setEnabled(enabled);
		if(changeFileNameListeners.size() < 1){
			buttonChangeFileName.setEnabled(false);
		}
	}
//	public void setButtonOpenEnbaled(boolean enabled){
//		buttonOpen.setEnabled(enabled);
//		if(openFileListeners.size() < 1){
//			buttonOpen.setEnabled(false);
//		}
//	}
	public void setButtonSaveEnbaled(boolean enabled){
		buttonSave.setEnabled(enabled);
		if(saveFileListeners.size() < 1){
			buttonSave.setEnabled(false);
		}
	}
	
	private List<ActionListener> changeFileNameListeners = new ArrayList<ActionListener>();  //  @jve:decl-index=0:
	public boolean registerChangeFileNameListener(ActionListener listener){
		return changeFileNameListeners.add(listener);
	}
	public boolean deRegisterChangeFileNameListener(ActionListener listener){
		return changeFileNameListeners.remove(listener);
	}
	private class ChangeFileNameAdvancer extends SelectionAdapter{
		@Override
		public void widgetSelected(SelectionEvent e) {
			ActionEvent event = new ActionEvent(CompositeFileConfig.this, ActionEvent.ACTION_PERFORMED, "ChangeFileName");
			for (ActionListener listener : changeFileNameListeners) {
				listener.actionPerformed(event);
			}
		}		
	}
	
//	private List<ActionListener> openFileListeners = new ArrayList<ActionListener>();
//	public boolean registerOpenFileListener(ActionListener listener){
//		return openFileListeners.add(listener);
//	}
//	public boolean deRegisterOpenFileListener(ActionListener listener){
//		return openFileListeners.remove(listener);
//	}
//	private class OpenFileAdvancer extends SelectionAdapter{
//		@Override
//		public void widgetSelected(SelectionEvent e) {
//			ActionEvent event = new ActionEvent(CompositeFileConfig.this, ActionEvent.ACTION_PERFORMED, "OpenFile");
//			for (ActionListener listener : openFileListeners) {
//				listener.actionPerformed(event);
//			}
//		}		
//	}
	
	private List<ActionListener> saveFileListeners = new ArrayList<ActionListener>();  //  @jve:decl-index=0:
	public boolean registerSaveFileListener(ActionListener listener){
		return saveFileListeners.add(listener);
	}
	public boolean deRegisterSaveFileListener(ActionListener listener){
		return saveFileListeners.remove(listener);
	}
	private class SaveFileAdvancer extends SelectionAdapter{
		@Override
		public void widgetSelected(SelectionEvent e) {
			ActionEvent event = new ActionEvent(CompositeFileConfig.this, ActionEvent.ACTION_PERFORMED, "SaveFile");
			for (ActionListener listener : saveFileListeners) {
				listener.actionPerformed(event);
			}
		}		
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"
